﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;

namespace WastedMechanics.CharState
{
    public abstract class CharStateJumpStand : CharState
    {
        enum JumpPhases
        {
            Pushing,
            Starting,
            Flying
        }

        JumpPhases JumpPhase;
        string AnimationName;

        public CharStateJumpStand(Character chr, 
            string animName)
            : base(chr)
        {
            AnimationName = animName;
        }

        public override void Enter()
        {
            Character.ResetAnimation(AnimationName);
            Character.SetAnimation(AnimationName);
            Character.CanTurn = false;
            JumpPhase = JumpPhases.Pushing;
        }

        public override void Exit()
        {
            Character.IsJumping = false;
        }

        public override void MoveOrder(bool run)
        {
            if (JumpPhase == JumpPhases.Pushing)
            {
                if (run)
                    Character.SwitchState(GetRunningState());
                else
                    Character.SwitchState(GetWalkingState());
            }
        }

        public override void Update()
        {
            switch (JumpPhase)
            {
                case JumpPhases.Pushing:
                    if (Character.AnimBlender.Target.TimePosition > Character.AnimBlender.Target.Length * 0.3f)
                    {
                        Character.Body.AddImpulse(Character.Orientation * new Vector3(0, 9f, 9f), Character.Position);
                        JumpPhase = JumpPhases.Starting;
                        Character.IsJumping = true;
                        Character.CanTurn = true;
                    }
                    break;
                case JumpPhases.Starting:
                    if (Character.IsOnGround == false)
                        JumpPhase = JumpPhases.Flying;
                    else if (Character.AnimBlender.Target.TimePosition > Character.AnimBlender.Target.Length * 0.95f)
                        Character.SwitchState(GetIdleState());
                    break;
                case JumpPhases.Flying:
                    if (Character.IsOnGround)
                        Character.SwitchState(GetLandingState());
                    break;
            }
        }

        public abstract CharState GetIdleState();
        public abstract CharState GetRunningState();
        public abstract CharState GetWalkingState();
        public abstract CharState GetLandingState();
    }
}
